package ebcs.scan.controller.backManagerCtr;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.RegionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import ebcs.database.bean.BsComment;
import ebcs.database.bean.BsFundsApply;
import ebcs.database.bean.BsGroupInfor;
import ebcs.database.bean.BsNeedInfor;
import ebcs.database.bean.BsNeedStage;
import ebcs.database.bean.BsProTeamInfor;
import ebcs.database.bean.BsUserDetailInfo;
import ebcs.database.bean.BsUserInfor;
import ebcs.database.bean.FaceToFace;
import ebcs.database.bean.ProjectCostInfo;
import ebcs.database.bean.RpCaptalReportBean;
import ebcs.database.bean.RpPreDictionBean;
import ebcs.database.bean.RpProjectMoneyBean;
import ebcs.database.bean.RpUserReportBean;
import ebcs.database.bean.SysUserFunc;
import ebcs.database.service.BsFundsApplyServiceImp;
import ebcs.database.service.BsGroupInforServiceImp;
import ebcs.database.service.BsProTeamServiceImp;
import ebcs.database.service.BsUserNeedServiceImp;
import ebcs.database.service.ProjectCostServiceImp;
import ebcs.database.service.ReportServiceImp;
import ebcs.database.service.SystemServiceImp;
import ebcs.database.service.impl.BsProTeamServiceImpl;
import ebcs.database.service.impl.BsUserNeedServiceImpl;
import ebcs.database.service.impl.ReportServiceImpl;
import ebcs.scan.bean.common.ExcleEntity;
import ebcs.scan.common.consts.UserStatus;
import ebcs.scan.common.utils.ExportDataUtil;
import ebcs.scan.common.utils.StringUtil;
import ebcs.scan.service.wechat.WeChatTemplPushService;
import ebcs.scan.service.wechat.WechatSignTools;
import jplat.core.trans.JAppBaseService;
import jplat.core.trans.JAppContext;
import jplat.core.trans.JAppContextFactory;
import jplat.error.exception.JSystemException;
import jplat.error.exception.JTransException;
import jplat.tools.config.JAppConfig;
import jplat.tools.config.JBusConfig;
import jplat.tools.string.DateUtil;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

/***
 * 报表管理
 * 
 * @author chenzhuo
 * @comment
 */
@Controller
public class ReportManagerCtrl extends JAppBaseService {
	private Logger logger = Logger.getLogger(ReportManagerCtrl.class);

	protected JAppContextFactory serviceFactory = new JAppContextFactory();

	// 数据库库服务.
	@Autowired
	public BsProTeamServiceImp teamService;
	@Autowired
	public BsGroupInforServiceImp groupService;
	@Autowired
	public BsUserNeedServiceImp userNeedService;
	@Autowired
	public ReportServiceImp reportService;
	@Autowired
	public SystemServiceImp systemService;
	@Autowired
	private WeChatTemplPushService weChatTemplPushService;
	@Autowired
	private ProjectCostServiceImp projectCostService;
	@Autowired
	private BsFundsApplyServiceImp bsFundsApplyServiceImp;
	
	public static String CHARSET_ENCODING = "UTF-8";
	RpProjectMoneyBean dzwList=null;
	/**
	 * 登录
	 */
	@RequestMapping("/page/main/manager.do")
	public String doIndex(HttpServletRequest request, HttpServletResponse response)
			throws JTransException, JSystemException {
		String state = request.getParameter("state");
		if(state == null || "".equals(state)){
			return "main/index";
		}
		request.getSession().setAttribute("loginUser", null);
		return "redirect:/page/main/manager.do";
	}
	/**
	 * 报表查询页面
	 */
	@RequestMapping("/page/sys/{reportId}/report.do")
	public String report(HttpServletRequest request, HttpServletResponse response, HttpSession session,@PathVariable String reportId)
			throws JTransException, JSystemException {
		//List<BsProTeamInfor> teanList = teamService.searchProTeanList();
		//List<BsGroupInfor> groupList = this.groupService.searchGroupList();
		
		Object list =  session.getAttribute("loginGroupInfo");
		BsUserInfor user=(BsUserInfor)session.getAttribute("loginUser");
		String position_type = user.getPosition_type();
		request.setAttribute("positionType", position_type);
		//如果是人力资源的想访问项目成本导入功能跳回确认密码页面
		if("18".equals(reportId)&&"103".equals(user.getProTeamNo().substring(0,3))){
			request.setAttribute("phoneNo", user.getPhoneNo());
			return "frame/html/user/reLogin" ;
		}
		if("18PW".equals(reportId)){
			reportId=reportId.substring(0, 2);
		}
		//当session中没有登录用户的团队经理信息那么就跳转到登录页面
		if(list == null || "".equals(list)){
			return "main/mainLogin";
		}
		List<BsGroupInfor> bsGroupInfors = (List<BsGroupInfor>) list;
		//防止position_type为空带来报错一律改为“x”.equals(position_type)
		if(bsGroupInfors.size() <= 0&&! "87".equals(position_type)&&!"1".equals(position_type)){
			return "main/mainLogin";
		}
		//根据团队经理信息查询出该团队经理下面所有的项目组信息
		String proGroupNo = "";
		String proTeamName = "";
		if(bsGroupInfors.size() > 0){
			 proGroupNo = bsGroupInfors.get(0).getProGroupNo();
			 proTeamName = bsGroupInfors.get(0).getProTeamName();
		}
		
		
		//如果是公共团队的话就可以查询出所有的团队 人力资源的话也查询所有的团队  ,如果是其他团队经理 查询该团队 如果是项目经理 查询该项目(yf  2018.10.15)
		//if(proGroupNo != null && "10906".equals(proGroupNo)){
		if(position_type!=null&&"69".equals(position_type)||"68".equals(position_type)||"101".equals(position_type)||"103".equals(position_type)||"109".equals(position_type)||"87".equals(position_type)||"1".equals(position_type)||"财务部" .equals(proTeamName)||"人力资源部" .equals(proTeamName)){
			if("101".equals(position_type)||"103".equals(position_type)||"109".equals(position_type)||"人力资源部" .equals(proTeamName) ||"财务部" .equals(proTeamName)|| "10906".equals(proGroupNo)||"68".equals(position_type)||"1".equals(position_type)||"87".equals(position_type)){
				List<BsProTeamInfor> teanList = teamService.searchProTeanList2(proGroupNo);
				List<BsGroupInfor> groupList = this.groupService.searchGroupList();
				request.setAttribute("teanList", teanList);
				request.setAttribute("groupList", groupList);
			}else{
				List<BsProTeamInfor> teanList = this.teamService.searchProTeanListByGroupNo(proGroupNo);
				request.setAttribute("teanList", teanList);
				request.setAttribute("groupList", list);
			}
		}else{
			List<BsProTeamInfor> teanList =this.teamService.searchProTeanListByProTeamNo(user.getProTeamNo());
			request.setAttribute("teanList", teanList);
			request.setAttribute("groupList", list);
		}
		request.setAttribute("sysdate", DateUtil.getSystemTimeBySomeFormat("yyyyMMdd"));
		return "main/report/report" + reportId;
	}

	/**
	 * 查询产能排行榜
	 */
	@RequestMapping("/page/sys/{proTeamNo}/selectCapacityRankingList.do")
	public String selectCapacityRankingList(HttpServletRequest request, HttpServletResponse response,
			@PathVariable String proTeamNo) throws JTransException, JSystemException {
		RpUserReportBean condition = new RpUserReportBean();
		condition.setProTeamNo(proTeamNo);
		List<RpUserReportBean> rpList = this.reportService.selectCapacityRankingList(condition);
		request.setAttribute("rpList", rpList);
		return "main/report/reportCap";

	}

	/**
	 * 查询用户产能排行榜
	 */
	@RequestMapping("/page/sys/selectUserCapacityRankingList.do")
	public String selectUserCapacityRankingList(HttpServletRequest request, HttpServletResponse response)
			throws JTransException, JSystemException {
		JAppContext context = serviceFactory.buildAppContextWithUser(request, response);// 自动客户信息
		BsUserInfor user = context.getUserInfo();

		List<SysUserFunc> userFunList = this.systemService.selectUserFunction(user);
		RpUserReportBean condition = new RpUserReportBean();
		if (userFunList.size() > 0) {
			condition.setProGroupNo(user.getProGroupNo());
		} else {
			condition.setProTeamNo(user.getProTeamNo());
		}
		List<RpUserReportBean> rpList = this.reportService.selectCapacityRankingList(condition);
		request.setAttribute("rpList", rpList);
		return "main/report/reportCap";

	}

	/**
	 * 根据团队编号查询项目组列表
	 */
	@RequestMapping("/page/main/doExcelFile.do")
	public void doExcelFile(HttpServletRequest request, HttpServletResponse response)
			throws JTransException, JSystemException {
		JAppContext context = serviceFactory.buildAppContextNoUser(request, response);// 自动客户信息
		RpUserReportBean param = context.obtainReqModel(RpUserReportBean.class);
		RpUserReportBean record = new RpUserReportBean();
		record.setProGroupNo(param.getProGroupNo());
		record.setProTeamNo(param.getProTeamNo());
		record.setStartDate(param.getStartDate());
		record.setEndDate(param.getEndDate());
		record.setReportType(param.getReportType());
		List<RpUserReportBean> rsList = userNeedService.searchUserNeedInfor(record);

		String path = request.getServletContext().getRealPath("");// 获取项目动态绝对路径
		ExportDataUtil export = new ExportDataUtil();
		ExcleEntity entity = new ExcleEntity();
		entity.setTitle("未报工信息列表");
		entity.setFileName("noTask" + DateUtil.getSystemTimeBySomeFormat(DateUtil.FMT_ALL) + ".xls");
		entity.setPath(path);
		Map<Object, String> headMap = new LinkedHashMap<Object, String>();
		headMap.put("proGroupName", "团队名称");
		headMap.put("proTeamName", "项目组名称");
		headMap.put("userName", "姓名");
		headMap.put("happenDate", "异常日期");
		headMap.put("needUseTime", "填报消耗时间(小时)");
		entity.setHeadMap(headMap);
		export.exportExcel(entity, rsList);
		context.buildAppResponse(entity);
	}

	/**
	 * 根据团队编号查询项目组列表
	 */
	@RequestMapping("/page/main/{groupNo}/searchTeamList.do")
	public void searchTeamList(HttpServletRequest request, HttpServletResponse response, @PathVariable String groupNo)
			throws JTransException, JSystemException {
		JAppContext context = serviceFactory.buildAppContextNoUser(request, response);// 自动客户信息
		List<BsProTeamInfor> teanList = this.teamService.searchProTeanListByGroupNo(groupNo);
		Map rsModel = new HashMap();
		rsModel.put("teanList", teanList);
		context.buildAppResponse(rsModel);
	}

	/**
	 * 查询当月报工情况用于统计数据 yf 
	 */
	@RequestMapping("/page/need/curMonthreportTask.do")
	public void reportTask(HttpServletRequest request, HttpServletResponse response)
			throws JTransException, JSystemException {
		JAppContext context = serviceFactory.buildAppContextNoUser(request, response);// 自动客户信息
		RpUserReportBean param = context.obtainReqModel(RpUserReportBean.class);
		context.obtainReqModel(RpUserReportBean.class);
		//查询当月报工异常的人员信息
		//List<RpUserReportBean> rsNoList = reportService.curMonthNoreportTask(param);
		//查询当月报工正常和报工异常的人员信息
		List<RpUserReportBean> rsList=  reportService.curMonthreportTask(param);
		List list=new ArrayList();
		List list2=new ArrayList();
		if(rsList!=null&&rsList.size()>0){
			for (RpUserReportBean rp : rsList) {
				if(rp.getUserName()!=null){
					if(rp.getUserName().equals(rp.getNeedName())){
						list.add(rp.getUserName());
						list.add(rp.getCountReport());
						list.add(rp.getCountNoReport());
					}
				}
				if(rp.getUserName()!=null&&rp.getNeedName()==null){
					list.add(rp.getUserName());
					list.add(rp.getCountReport());
					list.add(0);
				}
				if(rp.getUserName()==null&&rp.getNeedName()!=null){
					list.add(rp.getNeedName());
					list.add(0);
					list.add(rp.getCountNoReport());
				}
				list2.add(list);
				list=new ArrayList();;
			}
		}
		
		Map rsModel = new HashMap();
  		rsModel.put("list", list2);
  		//rsModel.put("rsNoList", rsNoList);
 		context.buildAppResponse(rsModel);
	}
	
	/**
	 * 报工情况统计
	 */
	@RequestMapping("/page/main/selectHavingTaskInfor.do")
	public void reportCount(HttpServletRequest request, HttpServletResponse response)
			throws JTransException, JSystemException {
		JAppContext context = serviceFactory.buildAppContextNoUser(request, response);// 自动客户信息
		RpUserReportBean param = context.obtainReqModel(RpUserReportBean.class);
		context.obtainReqModel(RpUserReportBean.class);
		String endRow = ((Integer.parseInt(param.getStartRow()))+(Integer.parseInt(param.getPageSize())))+"";
		param.setEndRow(endRow);
		List<RpUserReportBean> rsList = reportService.selectHavingTaskInforPage(param);
		int count =this.reportService.selectHavingTaskInforPageCount(param);
		Map rsModel = new HashMap();
		rsModel.put("count", count);
  		rsModel.put("rsList", rsList);
 		context.buildAppResponse(rsModel);
	}

	/**
	 * 未报工情况统计
	 */
	@RequestMapping("/page/main/reportNoTask.do")
	public void reportNoTask(HttpServletRequest request, HttpServletResponse response)
			throws JTransException, JSystemException {
		JAppContext context = serviceFactory.buildAppContextNoUser(request, response);// 自动客户信息
		RpUserReportBean param = context.obtainReqModel(RpUserReportBean.class);
		RpUserReportBean record = new RpUserReportBean();
		record.setProGroupNo(param.getProGroupNo());
		record.setProTeamNo(param.getProTeamNo());
		record.setStartDate(param.getStartDate());
		record.setEndDate(param.getEndDate());
		record.setReportType(param.getReportType());
		String endRow = ((Integer.parseInt(param.getStartRow()))+(Integer.parseInt(param.getPageSize())))+"";
		record.setEndRow(endRow);
		record.setStartRow(param.getStartRow());
		List<RpUserReportBean> rsList = userNeedService.searchUserNeedInforPage(record);
		int count = this.userNeedService.searchUserNeedInforPageCount(record);
		Map rsModel = new HashMap();
		rsModel.put("count", count);
  		rsModel.put("rsList", rsList);
 		context.buildAppResponse(rsModel);
	}

	/**
	 * 需求预期报表统计【查下】
	 */
	@RequestMapping("/page/main/needPreDiction.do")
	public void needPreDiction(HttpServletRequest request, HttpServletResponse response)
			throws JTransException, JSystemException {
		JAppContext context = serviceFactory.buildAppContextNoUser(request, response);// 自动客户信息
		RpUserReportBean param = context.obtainReqModel(RpUserReportBean.class);
		RpUserReportBean record = new RpUserReportBean();
		record.setProGroupNo(param.getProGroupNo());
		record.setProTeamNo(param.getProTeamNo());
		record.setNeedNo(param.getNeedNo());
		record.setNeedName(param.getNeedName());
		record.setStage(param.getStage());
		String endRow = ((Integer.parseInt(param.getStartRow()))+(Integer.parseInt(param.getPageSize())))+"";
		record.setEndRow(endRow);
		record.setStartRow(param.getStartRow());
		List<RpPreDictionBean> rsList = this.reportService.selectPreDictionListPage(record);
		BsNeedStage hh=new BsNeedStage();
		BsNeedStage xx=new BsNeedStage();
		String sysDate = DateUtil.getSystemTimeBySomeFormat("yyyyMMdd"); 
		for (RpPreDictionBean rr : rsList) {
			hh.setNeedId(rr.getNeedId());
			List<BsNeedStage> InfoList=userNeedService.selectBsNeedStage(hh);
			if(InfoList.size()>0) {
			xx=InfoList.get(0);
					if(rr.getStage().equals("需求酝酿")) {
						rr.setNow_time(xx.getNeed_Brewing());
						rr.setNew_time(sysDate);
					}else if(rr.getStage().equals("需求分析")) {
						rr.setNow_time(xx.getNeed_Analysis());
						rr.setNew_time(sysDate);
					}else if(rr.getStage().equals("需求确认")) {
						rr.setNow_time(xx.getNeed_Confirm());
						rr.setNew_time(sysDate);
					}else if(rr.getStage().equals("需求评审")) {
						rr.setNow_time(xx.getNeed_Review());
						rr.setNew_time(sysDate);
					}else if(rr.getStage().equals("技术评审")) {
						rr.setNow_time(xx.getTechnical_Review());
						rr.setNew_time(sysDate);
					}else if(rr.getStage().equals("开发联调")) {
						rr.setNow_time(xx.getDevelop_Joint());
						rr.setNew_time(sysDate);
					}else if(rr.getStage().equals("SIT测试")) {
						rr.setNow_time(xx.getSit_Test());
						rr.setNew_time(sysDate);
					}else if(rr.getStage().equals("UAT测试")) {
						rr.setNow_time(xx.getUat_Test());
						rr.setNew_time(sysDate);
					}else if(rr.getStage().equals("上线")) {
						rr.setNow_time(xx.getNeed_Online());
						rr.setNew_time(sysDate);
					}else if(rr.getStage().equals("回顾总结")) {
						rr.setNow_time(xx.getBack_Review());
						rr.setNew_time(sysDate);
					} 
			}
		
		}
		int count = this.reportService.selectPreDictionListPageCount(record);
		Map rsModel = new HashMap();
		rsModel.put("count", count);
  		rsModel.put("rsList", rsList);
 		context.buildAppResponse(rsModel);
	}
	
	/**
	 * 已报功情况统计【导出】
	 */
	@RequestMapping("/page/main/doReportCountExcelFile.do")
	public void doReportCountExcelFile(HttpServletRequest request, HttpServletResponse response)
			throws JTransException, JSystemException {
		JAppContext context = serviceFactory.buildAppContextNoUser(request, response);// 自动客户信息
		RpUserReportBean param = context.obtainReqModel(RpUserReportBean.class);

		List<RpUserReportBean> rsList = reportService.selectHavingTaskInfor(param);
		String path = request.getServletContext().getRealPath("");// 获取项目动态绝对路径
		ExportDataUtil export = new ExportDataUtil();
		ExcleEntity entity = new ExcleEntity();
		entity.setTitle("已报功情况统计");
		entity.setFileName(DateUtil.getSystemTimeBySomeFormat(DateUtil.FMT_ALL) + ".xls");
		entity.setPath(path);
		Map<Object, String> headMap = new LinkedHashMap<Object, String>();
		headMap.put("proGroupName","团队名称");
		headMap.put("proTeamName","项目组");
		headMap.put("userName","参与人员");
		headMap.put("needNo","需求编号");
		headMap.put("needName","需求名称");
		headMap.put("needUseTime","消耗工时");
		headMap.put("stage","状态");
		headMap.put("happenDate","登记时间");
		headMap.put("remark","备注");
		headMap.put("reportState","报功审核状态");
		entity.setHeadMap(headMap);
		export.exportExcel(entity, rsList);
		context.buildAppResponse(entity);
	}
	
	/**
	 * 需求预期报表统计【导出】
	 */
	@RequestMapping("/page/main/doPreDictionExcelFile.do")
	public void doPreDictionExcelFile(HttpServletRequest request, HttpServletResponse response)
			throws JTransException, JSystemException {
		JAppContext context = serviceFactory.buildAppContextNoUser(request, response);// 自动客户信息
		RpUserReportBean param = context.obtainReqModel(RpUserReportBean.class);
		RpUserReportBean record = new RpUserReportBean();
		record.setProGroupNo(param.getProGroupNo());
		record.setProTeamNo(param.getProTeamNo());
		record.setNeedNo(param.getNeedNo());
		record.setNeedName(param.getNeedName());
		record.setStage(param.getStage());
		List<RpPreDictionBean> rsList = this.reportService.selectPreDictionList(record);

		String path = request.getServletContext().getRealPath("");// 获取项目动态绝对路径
		ExportDataUtil export = new ExportDataUtil();
		ExcleEntity entity = new ExcleEntity();
		entity.setTitle("需求预期报表统计");
		entity.setFileName(DateUtil.getSystemTimeBySomeFormat(DateUtil.FMT_ALL) + ".xls");
		entity.setPath(path);
		Map<Object, String> headMap = new LinkedHashMap<Object, String>();
		headMap.put("proGroupName", "团队名称");
		headMap.put("proTeamName", "项目名称");
		headMap.put("needNo", "需求编号");
		headMap.put("needName", "需求名称");
		headMap.put("userName", "参与人员");
		headMap.put("endWorkDate", "最新报工日期");
		headMap.put("needTime", "需求评估人月");
		headMap.put("baseNeedTime", "成本控制人月");
		headMap.put("needUseTime", "需求消耗人月");
		headMap.put("stage", "最新需求阶段");
		headMap.put("state", "需求状态");
		headMap.put("ycqk", "预测完成时间");
		headMap.put("ycbl", "预测完成比例");
		entity.setHeadMap(headMap);
		export.exportExcel(entity, rsList);
		context.buildAppResponse(entity);
	}

	/**
	 * 项目组团队产能报表
	 */
	@RequestMapping("/page/main/selectCapReportList.do")
	public void selectCapReportList(HttpServletRequest request, HttpServletResponse response)
			throws JTransException, JSystemException {
		JAppContext context = serviceFactory.buildAppContextNoUser(request, response);// 自动客户信息
		RpUserReportBean param = context.obtainReqModel(RpUserReportBean.class);
		RpUserReportBean record = new RpUserReportBean();
		record.setProGroupNo(param.getProGroupNo());
		record.setProTeamNo(param.getProTeamNo());
		List<RpCaptalReportBean> rsList = this.reportService.selectCapReportList(record);
		context.buildAppResponse(rsList);
	}

	/**
	 * 需求进度实时分析
	 */
	@RequestMapping("/page/main/cycReport.do")
	public void cycReport(HttpServletRequest request, HttpServletResponse response)
			throws JTransException, JSystemException {
		JAppContext context = serviceFactory.buildAppContextNoUser(request, response);// 自动客户信息
		RpUserReportBean record = new RpUserReportBean();

		List<RpUserReportBean> rsList = userNeedService.selectReportCount(record);
		context.buildAppResponse(rsList);
	}

	/**
	 * 人员单产实时分析
	 */
	@RequestMapping("/page/main/reportPerson.do")
	public void reportPerson(HttpServletRequest request, HttpServletResponse response)
			throws JTransException, JSystemException {
		JAppContext context = serviceFactory.buildAppContextWithUser(request, response);// 自动客户信息
		BsUserInfor custModel = context.getUserInfo();
	}

	/**
	 * 发送出勤率大于60%表格给团队经理
	 * 
	 * @return
	 * @throws JSystemException
	 */
	@RequestMapping("/page/main/reportLtSixty")
	public String reportLtSixty(HttpServletRequest request, HttpServletResponse response) throws JSystemException {
		// 查询出出勤率大于60%的数据放到request中
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		request.setAttribute("reportDate", sdf.format(new Date()));
		RpUserReportBean record = new RpUserReportBean();
		record.setProGroupNo(request.getParameter("proGroupNo"));
		List<RpPreDictionBean> rsList = this.reportService.selectCapReportLt60(record);
		request.setAttribute("rsList", rsList);
		return "main/report/report9";
	}

	/**
	 * 发送出勤率大于60%表格给团队经理
	 * 
	 * @return
	 * @throws JSystemException
	 */
	@RequestMapping("/page/main/reportAllLtSixty")
	public String reportAllLtSixty(HttpServletRequest request, HttpServletResponse response) throws JSystemException {
		// 查询出出勤率大于60%的数据放到request中
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		request.setAttribute("reportDate", sdf.format(new Date()));
		RpUserReportBean record = new RpUserReportBean();
		List<RpPreDictionBean> rsList = this.reportService.selectCapAllReportLt60();
		request.setAttribute("rsList", rsList);
		return "main/report/report9";
	}

	/**
	 * 
	 * @param url
	 *            调用此方法的页面
	 * @return 返回微信配置的数据
	 */
	
	@RequestMapping("/page/main/wechatConfig.do")
	public void  getWechatConfig(@RequestParam (value="url")String url ,HttpServletRequest request, HttpServletResponse response)
			throws JTransException, JSystemException {
		JAppContext context = serviceFactory.buildAppContextNoUser(request, response);// 自动客户信息
		Map<String, String> ret = weChatTemplPushService.getWechatConfig(url);
		context.buildAppResponse(ret);
	}
	/**
	 * 项目奖金分析计算查询
	 * 
	 * @param request
	 * @param response
	 * @throws JTransException
	 * @throws JSystemException
	 */
	@RequestMapping("/page/main/selectProjectMoneyList.do")
	public void selectProjectMoneyList(HttpServletRequest request, HttpServletResponse response)
			throws JTransException, JSystemException {
		JAppContext context = serviceFactory.buildAppContextNoUser(request, response);// 自动客户信息
		RpProjectMoneyBean param = context.obtainReqModel(RpProjectMoneyBean.class);
		RpProjectMoneyBean record = new RpProjectMoneyBean();
		record.setProGroupNo(param.getProGroupNo());
		record.setProTeamNo(param.getProTeamNo());
		record.setTotal(param.getTotal());
		record.setNeedName(param.getNeedName());
		record.setNeedNo(param.getNeedNo());
		record.setBgnWorkDate(param.getBgnWorkDate());
		record.setEndWorkDate(param.getEndWorkDate());
		record.setState(param.getState());
		record.setContractIdSign(param.getContractIdSign());
		record.setContractNoSign(param.getContractNoSign());
		String endRow = ((Integer.parseInt(param.getStartRow()))+(Integer.parseInt(param.getPageSize())))+"";
		record.setEndRow(endRow);
		record.setStartRow(param.getStartRow());
		List<RpProjectMoneyBean> rpList = reportService.selectProjectMoneyListPage(record);
		RpProjectMoneyBean con = this.reportService.selectProjectMoneyListPageCount(record);
		Map rsModel = new HashMap();
		rsModel.put("sumPeopleDay", con.getSum_People_Day());
		rsModel.put("count", con.getCount());
  		rsModel.put("rpList", rpList);
 		context.buildAppResponse(rsModel);
	}
	
	/**
	 * 项目奖金分析计算查询
	 * 
	 * @param request
	 * @param response
	 * @throws JTransException
	 * @throws JSystemException
	 */
	@RequestMapping("/page/main/selectNeedUseList.do")
	public void selectNeedUseList(HttpServletRequest request, HttpServletResponse response)
			throws JTransException, JSystemException {
		JAppContext context = serviceFactory.buildAppContextNoUser(request, response);// 自动客户信息
		RpProjectMoneyBean param = context.obtainReqModel(RpProjectMoneyBean.class);
		RpProjectMoneyBean record = new RpProjectMoneyBean();
		record.setProGroupNo(param.getProGroupNo());
		record.setProTeamNo(param.getProTeamNo());
		record.setNeedName(param.getNeedName());
		record.setNeedNo(param.getNeedNo());
		record.setBgnWorkDate(param.getBgnWorkDate());
		record.setEndWorkDate(param.getEndWorkDate());
		String endRow = ((Integer.parseInt(param.getStartRow()))+(Integer.parseInt(param.getPageSize())))+"";
		record.setEndRow(endRow);
		record.setStartRow(param.getStartRow());
		List<RpProjectMoneyBean> rpList = reportService.selectNeedUseListPage(record);
		int count  = this.reportService.selectNeedUseListCount(record);
		Map rsModel = new HashMap();
		rsModel.put("count", count);
  		rsModel.put("rpList", rpList);
 		context.buildAppResponse(rsModel);
	}
	
	/**
	 * 项目奖励表
	 * 
	 * @param request
	 * @param response
	 * @throws JTransException
	 * @throws JSystemException
	 */
	@RequestMapping("/page/need/doJiangjinChaKan.do")
	public void doJiangjinChaKan(HttpServletRequest request, HttpServletResponse response)
			throws JTransException, JSystemException {
		JAppContext context = serviceFactory.buildAppContextNoUser(request, response);// 自动客户信息
		RpProjectMoneyBean param = context.obtainReqModel(RpProjectMoneyBean.class);
		List<RpProjectMoneyBean> mlList = reportService.selectMlListProjectMoney(param);	// 项目毛利表
		List<RpProjectMoneyBean> pgList = reportService.selectPgListProjectMoney(param);	// 评估表
		List<RpProjectMoneyBean> jlList = reportService.selectJlListProjectMoney(param);	// 项目奖励表
		Map rsModel = new HashMap();
		rsModel.put("mlList", mlList);
		rsModel.put("pgList", pgList);
		rsModel.put("jlList", jlList);
		context.buildAppResponse(rsModel);
	}
	
	/**
	 * 项目奖励表
	 * 
	 * @param request
	 * @param response
	 * @throws JTransException
	 * @throws JSystemException
	 */
	@RequestMapping("/page/need/doChaKan.do")
	public String xiangmujiangli(HttpServletRequest request, HttpServletResponse response,String pro_group_no,String state)
			throws JTransException, JSystemException {
		JAppContext context = serviceFactory.buildAppContextWithUser(request, response);  
		BsUserInfor userBean = context.getUserInfo();
		request.setAttribute("positionType", userBean.getPosition_type());
		request.setAttribute("proGroupNo", pro_group_no);
		request.setAttribute("state", state);
		return "main/report/report15"; 
	}
	
	/**
	 * 项目奖励表通过
	 * 
	 * @param request
	 * @param response
	 * @throws JTransException
	 * @throws JSystemException
	 */
	@RequestMapping("/page/need/TongGuo.do")
	public void TongGuo(HttpServletRequest request, HttpServletResponse response, String rouidList)
			throws JTransException, JSystemException {
		JAppContext context = serviceFactory.buildAppContextWithUser(request, response);        //buildAppContextWithUser 自带客户信息.
		BsUserInfor userBean = context.getUserInfo();
		String positionType = userBean.getPosition_type();
		String[] rouide = rouidList.split(",");
        for(String rouid:rouide) {
        	BsFundsApply param = new BsFundsApply();
        	param.setRouid(rouid);
        	BsFundsApply BsFundsApply = bsFundsApplyServiceImp.selectApplyFundsByRouid(rouid);
        	if("101".equals(positionType)) {
        		param.setCw_state("01");
        		param.setRl_state(BsFundsApply.getRl_state());
        		if("01".equals(BsFundsApply.getRl_state())) {
        			param.setState("01");
        		}else {
        			param.setState(BsFundsApply.getState());
        		}
        	}else if("103".equals(positionType)){
        		param.setRl_state("01");
        		param.setCw_state(BsFundsApply.getCw_state());
        		if("01".equals(BsFundsApply.getCw_state())) {
        			param.setState("01");
        		}else {
        			param.setState(BsFundsApply.getState());
        		}
        	}
        	this.bsFundsApplyServiceImp.tongGuoNeedById(param);
        }
		Map rsModel = new HashMap();
 		context.buildAppResponse(rsModel);
		//context.buildAppResponse(rpList);
	}
	
	
	/**
	 * 项目奖励表驳回
	 * 
	 * @param request
	 * @param response
	 * @throws JTransException
	 * @throws JSystemException
	 */
	@RequestMapping("/page/need/BoHui.do")
	public void BoHui(HttpServletRequest request, HttpServletResponse response, String rouidList)
			throws JTransException, JSystemException {
		JAppContext context = serviceFactory.buildAppContextWithUser(request, response);        //buildAppContextWithUser 自带客户信息.
		BsUserInfor userBean = context.getUserInfo();
		String positionType = userBean.getPosition_type();
		String[] rouide = rouidList.split(",");
        for(String rouid:rouide) {
        	BsFundsApply param = new BsFundsApply();
        	param.setRouid(rouid);
        	if("101".equals(positionType)) {
        		param.setCw_state("02");
    			param.setState("02");
        	}else if("103".equals(positionType)){
        		param.setRl_state("02");
    			param.setState("02");
        	}
        	this.bsFundsApplyServiceImp.boHuiNeedById(param);
        }
		Map rsModel = new HashMap();
 		context.buildAppResponse(rsModel);
		//context.buildAppResponse(rpList);
	}
	
	

	/**
	 * 项目奖金分析计算导出
	 * 
	 * @param request
	 * @param response
	 * @throws JTransException
	 * @throws JSystemException
	 */
	@RequestMapping("/page/main/doExcelFileOfProjectMoney.do")
	public void doExcelFileOfProjectMoney(HttpServletRequest request, HttpServletResponse response)
			throws JTransException, JSystemException {
		JAppContext context = serviceFactory.buildAppContextNoUser(request, response);// 自动客户信息
		RpProjectMoneyBean param = context.obtainReqModel(RpProjectMoneyBean.class);
		RpProjectMoneyBean record = new RpProjectMoneyBean();
		record.setProGroupNo(param.getProGroupNo());
		record.setProTeamNo(param.getProTeamNo());
		record.setTotal(param.getTotal());
		record.setNeedName(param.getNeedName());
		record.setNeedNo(param.getNeedNo());
		record.setBgnWorkDate(param.getBgnWorkDate());
		record.setEndWorkDate(param.getEndWorkDate());
		record.setState(param.getState());
		record.setContractIdSign(param.getContractIdSign());
		record.setContractNoSign(param.getContractNoSign());
		String path = request.getServletContext().getRealPath("");// 获取项目动态绝对路径
		ExportDataUtil export = new ExportDataUtil();
		ExcleEntity entity = new ExcleEntity();
		entity.setTitle("项目奖金计算分析列表");
		entity.setFileName("ProjectMoney" + DateUtil.getSystemTimeBySomeFormat(DateUtil.FMT_ALL) + ".xls");
		entity.setPath(path);
		Map<Object, String> headMap = new LinkedHashMap<Object, String>();
		headMap.put("proGroupName", "团队名称");
		headMap.put("proTeamName", "项目组");
		headMap.put("contractId", "合同号");
		headMap.put("contract_no", "产品编号");
		headMap.put("needNo", "需求编号");
		headMap.put("needName", "需求名称");
		headMap.put("userName", "参与人员");
		headMap.put("needTime", "评估工时");
		headMap.put("cbkzry", "成本控制人月");
		headMap.put("needUseTime", "需求消耗人月 ");
		headMap.put("rydc", "人员单产(万元)");
		headMap.put("bgnWorkDate", "开始报工日期");
		headMap.put("endWorkDate", "最新报工日期");
		headMap.put("stage", "最新需求阶段");
		headMap.put("state", "需求状态");
		headMap.put("ycqk", "分析结果");
		headMap.put("ycbl", "需求消耗/需求评估");
		headMap.put("dcfz", "单产分值");
		headMap.put("needUserBl", "人员结构比值");
		headMap.put("needuserscore", "人员结构比值得分");
		headMap.put("grade", "甲方评分");
		headMap.put("total", "总分值");
		entity.setHeadMap(headMap);
		List<RpProjectMoneyBean> rpList = reportService.selectProjectMoneyList(record);
		export.exportExcel(entity, rpList);
		context.buildAppResponse(entity);
	}

	/**
	 * 需求消耗情况导出
	 * 
	 * @param request
	 * @param response
	 * @throws JTransException
	 * @throws JSystemException
	 */
	@RequestMapping("/page/main/doExcelNeedUse.do")
	public void doExcelNeedUse(HttpServletRequest request, HttpServletResponse response)
			throws JTransException, JSystemException {
		JAppContext context = serviceFactory.buildAppContextNoUser(request, response);// 自动客户信息
		RpProjectMoneyBean param = context.obtainReqModel(RpProjectMoneyBean.class);
		RpProjectMoneyBean record = new RpProjectMoneyBean();
		record.setProGroupNo(param.getProGroupNo());
		record.setProTeamNo(param.getProTeamNo());
		record.setNeedName(param.getNeedName());
		record.setNeedNo(param.getNeedNo());
		record.setBgnWorkDate(param.getBgnWorkDate());
		record.setEndWorkDate(param.getEndWorkDate());
		List<RpProjectMoneyBean> rpList = reportService.selectNeedUseList(record);
		String path = request.getServletContext().getRealPath("");// 获取项目动态绝对路径
		ExportDataUtil export = new ExportDataUtil();
		ExcleEntity entity = new ExcleEntity();
		entity.setTitle("各团队需求消耗情况统计");
		entity.setFileName("需求消耗情况统计" + DateUtil.getSystemTimeBySomeFormat(DateUtil.FMT_ALL) + ".xls");
		entity.setPath(path);
		Map<Object, String> headMap = new LinkedHashMap<Object, String>();
		headMap.put("proGroupName", "团队名称");
		headMap.put("proTeamName", "项目组");
		headMap.put("needNo", "需求编号");
		headMap.put("needName", "需求名称");
		headMap.put("userName", "参与人员");
		headMap.put("needTime", "评估工时");
		headMap.put("stage", "需求阶段");
		headMap.put("state", "需求状态");
		headMap.put("bgnWorkDate", "开始报工日期");
		headMap.put("endWorkDate", "最新报工日期");
		headMap.put("happenMonth", "月份");
		headMap.put("needUseTime", "需求消耗人月 ");
        entity.setHeadMap(headMap);
		export.exportExcel(entity, rpList);
		context.buildAppResponse(entity);
	}
	
	
	
	
	/**
	 * 根据needId查询项目奖金分析计算二级信息
	 * 
	 * @param request
	 * @param response
	 * @param userNo
	 * @return
	 * @throws JTransException
	 * @throws JSystemException
	 */
	@RequestMapping("/page/main/{needId}/searchDetailByNeedId.do")
	public String searchDetailByNeedId(HttpServletRequest request, HttpServletResponse response,
			@PathVariable String needId) throws JTransException, JSystemException {
		List<RpProjectMoneyBean> detailInfoList = reportService.searchDetailByNeedId(needId);
		request.setAttribute("detailInfoList", detailInfoList);
		return "main/report/projectMoneyDetail";
	}
	/**
	 * 根据needId和userNO查询项目奖金分析计算三级信息
	 * 
	 * @param request
	 * @param response
	 * @param userNo
	 * @return
	 * @throws JTransException
	 * @throws JSystemException
	 */
	@RequestMapping("/page/main/{needId}/searchByNeedIdAndUserNo.do")
	public String searchByNeedIdAndUserNo(HttpServletRequest request, HttpServletResponse response,
			@PathVariable String needId,@RequestParam  String userNo) throws JTransException, JSystemException {
		RpUserReportBean record = new  RpUserReportBean();
		record.setUserNo(userNo);
		record.setRouid(needId);
		List<RpUserReportBean> rsList = userNeedService.searchUserHistoryNeedInfor(record);
		if(rsList!=null&&rsList.size()>0){
			request.setAttribute("userName", rsList.get(0).getUserName());
			request.setAttribute("proTeamName", rsList.get(0).getProTeamName());
			request.setAttribute("proGroupName", rsList.get(0).getProGroupName());
		}
 		request.setAttribute("detailInfoList", rsList);
		return "main/report/projectMoneyDetail2";
	}
    //合并对话框加边框	
	private static void setBorderForMergeCell(int i,CellRangeAddress cellRange,HSSFSheet sheet,HSSFWorkbook wb){
    	RegionUtil.setBorderBottom(i, cellRange, sheet, wb);
    	RegionUtil.setBorderLeft(i, cellRange, sheet, wb);
    	RegionUtil.setBorderRight(i, cellRange, sheet, wb);
    	RegionUtil.setBorderTop(i, cellRange, sheet, wb);
    }
	/**
	 * 项目奖金表下载
	 * @throws JSystemException 
	 */
	@RequestMapping("/page/main/load.do")
	public void load(HttpServletRequest request, HttpServletResponse response) throws JSystemException{
		JAppContext context = serviceFactory.buildAppContextNoUser(request, response);// 自动客户信息
		RpProjectMoneyBean param = context.obtainReqModel(RpProjectMoneyBean.class);
		
		String path = request.getServletContext().getRealPath("");// 获取项目动态绝对路径
		ExportDataUtil excel = new ExportDataUtil();
		ExcleEntity excleBean = new ExcleEntity();
		excleBean.setTitleArr(new String[] {"项目毛利表","评估表","项目奖金表"});
		excleBean.setFileName(DateUtil.getSystemTimeBySomeFormat(DateUtil.FMT_ALL) + "项目奖金表.xls");
		excleBean.setPath(path);
		List headList = new ArrayList();
		Map<Object, String> headMap = new LinkedHashMap<Object, String>();
		headMap.put("proGroupName","团队名称");
		headMap.put("proTeamName","项目组");
		headMap.put("needNo","需求编号");
		headMap.put("contractId","合同号");
		headMap.put("contract_no","产品编号");
		headMap.put("needName","需求名称");
		headMap.put("xmje","项目金额");
		headMap.put(" ","系统核定金额");
		headMap.put("paybackAmount","回款金额");
		headMap.put("sj","税金");
		headMap.put("xmcb","人力成本");
		headMap.put("projectFee","项目费用");
		headMap.put("xmml","项目毛利");
		/*headMap.put("groupmoney","团队总收入");
		headMap.put("allpeople","团队总人数");
		headMap.put("zrydc","人均产值");
		headMap.put("zxmcb","人力成本");*/
		headList.add(headMap);
		Map<Object, String> headMap2 = new LinkedHashMap<Object, String>();
		headMap2.put("proGroupName","团队名称");
		headMap2.put("proTeamName","项目组");
		headMap2.put("needNo","需求编号");
		headMap2.put("needName","需求名称");
		headMap2.put("paybackAmount","回款金额");
		headMap2.put("needUseTime","需求消耗人月");
		headMap2.put("rydc","人均产值");
		headMap2.put("dcfz","得分");
		headMap2.put("needUserBl","人员结构比例");
		headMap2.put("needuserscore","得分");
		headMap2.put("jiaresult","甲方评分");	
		headMap2.put("grade","得分");	
		headMap2.put("total","综合得分");	
		headList.add(headMap2);
		Map<Object, String> headMap1 = new LinkedHashMap<Object, String>();
		headMap1.put("proGroupName","团队名称");
		headMap1.put("proTeamName","项目组");
		headMap1.put("needNo","需求编号");
		headMap1.put("needName","需求名称");
		headMap1.put("xmml","项目毛利");
		headMap1.put("total","综合得分");
		headMap1.put("jjlv","对应奖金等级");
		headMap1.put("jjmoney","奖金金额");
		headList.add(headMap1);
		excleBean.setHeadList(headList);
		List<RpProjectMoneyBean> mlList = this.reportService.selectMlListProjectMoney(param);	// 项目毛利表
		List<RpProjectMoneyBean> pgList = this.reportService.selectPgListProjectMoney(param);	// 评估表
		List<RpProjectMoneyBean> jlList = this.reportService.selectJlListProjectMoney(param);	// 项目奖励表
		List[]reportListArr=new List[]{mlList,pgList,jlList};
		excel.exportExcelMoreSheet(excleBean, reportListArr);
		context.buildAppResponse(excleBean);
	}
	/**
	 * 导出
	 * @param request
	 * @param response
	 * @throws JTransException
	 * @throws JSystemException
	 */
	@RequestMapping("/page/main/out.do")
	public void doExcelFacetoFace(HttpServletRequest request, HttpServletResponse response)
			throws JTransException, JSystemException {
		JAppContext context = serviceFactory.buildAppContextNoUser(request, response);// 自动客户信息
		RpProjectMoneyBean param = context.obtainReqModel(RpProjectMoneyBean.class);

		List<RpProjectMoneyBean> projectReturnList = this.reportService.selectMlListProjectMoneyFunds(param);
		String path = request.getServletContext().getRealPath("");// 获取项目动态绝对路径
		ExportDataUtil export = new ExportDataUtil();
		ExcleEntity entity = new ExcleEntity();
		entity.setTitle("项目毛利表");
		entity.setFileName(DateUtil.getSystemTimeBySomeFormat(DateUtil.FMT_ALL) + "项目毛利表.xls");
		entity.setPath(path);
		Map<Object, String> headMap = new LinkedHashMap<Object, String>();
		headMap.put("proGroupName","团队名称");
		headMap.put("proTeamName","项目组");
		headMap.put("contractId","合同号");
		headMap.put("contract_no","产品编号");
		headMap.put("needNo","需求编号");
		headMap.put("xmje","项目金额");
		headMap.put("","系统核定金额");
		headMap.put("paybackAmount","回款金额");
		headMap.put("projectFee","项目费用");
		entity.setHeadMap(headMap);
		export.exportExcel(entity, projectReturnList);
		context.buildAppResponse(entity);
	}
	
	/**
	 * 根据needId查询项目奖金明细
	 * 
	 * @param request
	 * @param response
	 * @param userNo
	 * @return
	 * @throws JTransException
	 * @throws JSystemException
	 */
	@RequestMapping("/page/main/{needId}/{xmje}/{xthdje}/{sj}/MxByNeedId.do")
	public String MxByNeedId(HttpServletRequest request, HttpServletResponse response,
			@PathVariable String needId,@PathVariable String xmje,@PathVariable String xthdje,@PathVariable String sj) throws JTransException, JSystemException {
		RpProjectMoneyBean req =new RpProjectMoneyBean();
		req.setNeedId(needId);
		List<RpProjectMoneyBean> InfoList = reportService.MxByNeedId(req);
		request.setAttribute("InfoList", InfoList);
		request.setAttribute("xmje", xmje);
		request.setAttribute("xthdje", xthdje);
		request.setAttribute("sj", sj);
		return "main/report/projectMoneyMx";
	}
	
	
	

	 
	/**
	 * 保存项目奖金申请
	 */
	 @RequestMapping("/page/main/saveApplyFunds.do")
	 public void saveApplyFunds(HttpServletRequest request, HttpServletResponse response,String needId) throws JTransException, JSystemException{
		 JAppContext context = serviceFactory.buildAppContextNoUser(request, response);//自动客户信息
		 String[] needIdList = needId.split(",");
        for(String need:needIdList) {
        	if(bsFundsApplyServiceImp.selectApplyFunds(need).size()<=0) {
        		bsFundsApplyServiceImp.saveApplyFunds(need);
        	}else {
        		bsFundsApplyServiceImp.updateByNeedId(need);
        	}
        }
        Map rspModel = new HashMap();
        context.buildAppResponse(rspModel);
	 }
	 
	 /**
		 * 查询项目奖金申请
		 */
		@RequestMapping("/page/main/searchJiangJinShenqing.do")
		public void searchJiangJinShenqing(HttpServletRequest request, HttpServletResponse response)
				throws JTransException, JSystemException {
			JAppContext context = serviceFactory.buildAppContextNoUser(request, response);// 自动客户信息
			BsFundsApply req = context.obtainReqModel(BsFundsApply.class);
			List<BsFundsApply> rsList = bsFundsApplyServiceImp.searchChaKan(req);
	 		context.buildAppResponse(rsList);
		}
	 
	 
}
